package ${package_name}.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package_name}.core.common.BeanMapper;
import ${package_name}.core.common.PageRequest;
import ${package_name}.dto.PageResponseDto;
import ${package_name}.dto.${system_name}.${fun_name}.*;
import ${package_name}.entity.${table_entity.entityName};
import ${package_name}.service.${table_entity.entityName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author auto code
 */
@RestController
public class ${table_entity.entityName}Controller {

    @Autowired
    private ${table_entity.entityName}Service ${table_entity.aliasName}Service;


    @PostMapping("/${request_url}/listPage")
    public PageResponseDto listPage(@RequestBody PageRequest<${table_entity.entityName}QueryRequestDto> page) {
        Page<${table_entity.entityName}> resultPage = this.${table_entity.aliasName}Service.findPage(page.getPage(), page.getLimit(), BeanMapper.map(page.getCondition(), ${table_entity.entityName}.class));
        return PageResponseDto.builder().total(resultPage.getTotal()).data(BeanMapper.mapAsList(resultPage.getRecords(), ${table_entity.entityName}QueryResponseDto.class)).build();
    }


    @PostMapping("/${request_url}/add")
    public Long add${table_entity.entityName}(@RequestBody ${table_entity.entityName}AddRequestDto addDto) {
        return this.${table_entity.aliasName}Service.add${table_entity.entityName}(BeanMapper.map(addDto, ${table_entity.entityName}.class));
    }


    @PostMapping("/${request_url}/edit")
    public void edit${table_entity.entityName}(@RequestBody ${table_entity.entityName}EditRequestDto editDto) {
        this.${table_entity.aliasName}Service.update${table_entity.entityName}WithLock(BeanMapper.map(editDto, ${table_entity.entityName}.class));
    }


    @PostMapping("/${request_url}/del")
    public void del${table_entity.entityName}(@RequestBody ${table_entity.entityName}DelRequestDto delDto) {
        this.${table_entity.aliasName}Service.del${table_entity.entityName}(BeanMapper.map(delDto, ${table_entity.entityName}.class));
    }


    @PostMapping("/${request_url}/get")
    public ${table_entity.entityName}GetResponseDto findById(@RequestBody ${table_entity.entityName}GetRequestDto getDto) {
        QueryWrapper<${table_entity.entityName}> ${table_entity.aliasName}QueryWrapper = new QueryWrapper<>();
        ${table_entity.aliasName}QueryWrapper.setEntity(BeanMapper.map(getDto, ${table_entity.entityName}.class));
        ${table_entity.entityName} ${table_entity.aliasName} = this.${table_entity.aliasName}Service.getOne(${table_entity.aliasName}QueryWrapper);
        return null == ${table_entity.aliasName} ? null : BeanMapper.map(${table_entity.aliasName}, ${table_entity.entityName}GetResponseDto.class);
    }
}
